import wx from "weixin-js-sdk"
import { wxShare } from '../api';
import { WxAppid, share_url, share_title, share_desc, share_image } from "./config";

//使用时需要先调用checkWx
export const checkWx = async ()=>{
    let res = await wxShare(window.location.href.split('#')[0]);
    wx.config({
        debug: false, // 开启调试模式,
        appId: WxAppid, // 必填，企业号的唯一标识，此处填写企业号corpid
        timestamp: res.data.Timestamp, // 必填，生成签名的时间戳
        nonceStr: res.data.RandomString, // 必填，生成签名的随机串
        signature: res.data.Signature,// 必填，签名，见附录1
        jsApiList: [
            'onMenuShareAppMessage',
            'onMenuShareTimeline',
            'onMenuShareQQ',
            'onMenuShareWeibo',
            // 'updateAppMessageShareData',
            // 'updateTimelineShareData',
            // 'onMenuShareWeibo',
            'startRecord',
            'stopRecord',
            'uploadVoice',
            'onVoiceRecordEnd',
            'onVoicePlayEnd',
            'playVoice',
            'pauseVoice',
            'stopVoice',
            'downloadVoice',
            'translateVoice',
            //微信支付
            'chooseWXPay',
        ]
    });
}
export const Wshare = async ({
    title,
    desc,
    url,
    imgurl
}={
    title:share_title,
    desc:share_desc,
    url:share_url,
    imgurl:share_image
})=>{
    wx.ready(()=>{
        let shareTitle = title;
        let shareDesc = desc;
        let shareLink = url;
        let shareImgUrl = imgurl;
        wx.onMenuShareAppMessage({
            title: shareTitle,
            desc: shareDesc,
            link: shareLink,
            imgUrl: shareImgUrl,
            type:'link',
            trigger: function () {
            },
            success: function () {
            },
            cancel: function () {
            },
            fail: function () {
            }
        });

        // 2.2 监听“分享到朋友圈”按钮点击、自定义分享内容及分享结果接口
        wx.onMenuShareTimeline({
            title: shareTitle,
            link: shareLink,
            imgUrl: shareImgUrl,
            type:'link',
            trigger: function () {
            },
            success: function () {
            },
            cancel: function () {
            },
            fail: function () {
            }
        });

        // 2.3 监听“分享到QQ”按钮点击、自定义分享内容及分享结果接口
        wx.onMenuShareQQ({
            title: shareTitle,
            desc: shareDesc,
            link: shareLink,
            imgUrl: shareImgUrl,
            type:'link',
            trigger: function () {
            },
            complete: function () {
            },
            success: function () {
            },
            cancel: function () {
            },
            fail: function () {
            }
        });

        // 2.4 监听“分享到微博”按钮点击、自定义分享内容及分享结果接口
        wx.onMenuShareWeibo({
            title: shareTitle,
            desc: shareDesc,
            link: shareLink,
            imgUrl: shareImgUrl,
            type:'link',
            trigger: function () {
            },
            complete: function () {
            },
            success: function () {
            },
            cancel: function () {
            },
            fail: function () {
            }
        });
    })
}
export const weixin = wx;